<?php
namespace app\index\controller;

use think\Controller;
use app\index\model\Admin as Admin;
use think\Request;
use think\Cookie;

class Login extends Controller{
    //登录页面
    public function index(Request $request){
        if($request->param("uname")){
            $uname = $request->param("uname");
            $upass = md5($request->param("upass"));
            $remember_pass = $request->param("remember_pass");
            $result = Admin::get(["uname"=>$uname,"upass"=>$upass]);
            if($result['uname']&&$result['upass']){
                $auth = $result['uname']."-".md5($result['upass']);

                if($remember_pass){
                    Cookie::set("auth",$auth,3600*24*7);
                }else{
                    Cookie::set("auth",$auth,3600*20);
                }


                echo "ok";
            }else{
                echo "error";
            }
        }else{

            if(Cookie::has('auth')){
                $this->redirect("Index/index");
            }

            if(isMobile()){
                return $this->fetch("Login/mobile");
            }else{
                return $this->fetch("Login/index");
            }


        }
    }

    //登出页面
    public function logout(){
        Cookie::set("auth",null);
        $this->redirect("/index/login/index");
    }

}
